package lib.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import lib.Base.Settings;
import lib.Common.Path;
import lib.FileSystem.*;
import lib.XML.XMLWriter;

import org.swixml.SwingEngine;
import org.swixml.XDialog;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class GuiLogin  {
	/**
	 * Class which represents a Login window for the user
	 */
	private SwingEngine engine;
	private XDialog container;
	private JComboBox combo_user;
	private JPasswordField input_password;
	private JButton btn_login;
	private JButton btn_close;
	private Vector comboBoxItems;
	private Vector comboBoxItemsId;
	private boolean isFilled = false;
	
	public Action handleClick = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
        	// Check ActionCommand
        	if(e.getActionCommand().equals("Login")){
        		// Handle login
        		String fileName = comboBoxItemsId.get(combo_user.getSelectedIndex()).toString();
        		Make.CLEAR_DIR(Path.RES_DIR);
        		boolean wrong = false;
        		try{
	        		ZipFile zipFile = new ZipFile(Path.USERS_DIR+fileName);
	        		if(zipFile.isEncrypted()){
	        			zipFile.setPassword(String.valueOf(input_password.getPassword()));
	        		}
	        		zipFile.extractAll(Path.JSM_DIR);
        		}catch(ZipException z) {
        			wrong = true;
        			JOptionPane.showMessageDialog(null, "Password wrong", "Password Error", JOptionPane.ERROR_MESSAGE);
        		}
        		       		
        		if(!wrong) {
	        		container.dispose();
	        		container = null;
	        		engine = null;
	        		String[] splited = fileName.split("-");
	        		String ID = splited[0];
	        		String NAME = splited[1].split("\\.")[0];

	        		new GuiMain(NAME, ID, String.valueOf(input_password.getPassword()));
        		}
        	}else if(e.getActionCommand().equals("Exit")){
        		System.exit(1);
        	}else if(e.getActionCommand().equals("Register")){
        		container.dispose();
        		container = null;
        		engine = null;
        		new GuiRegistration();
        	}
        }
    };
    
    public void fillComboBox(){
    	File[] files = new File(Path.USERS_DIR).listFiles();

    	for (File file : files) {
    	    if (file.isFile()) {
    	    	String[] ext = file.getName().split("\\.");
    	    	String[] username = ext[0].split("-");
    	    	
    	    	if(ext[ext.length-1].equals("zip")){
    	    		comboBoxItems.add(username[1]);
    	    		comboBoxItemsId.add(file.getName());
    	    	}
    	    }
    	}
    	
    	if(combo_user.getModel() != null){
    		combo_user.setModel(new DefaultComboBoxModel(comboBoxItems));
    		new DefaultComboBoxModel();
    	}
    }

	public GuiLogin() {
		try {
			comboBoxItems = new Vector();
			comboBoxItemsId = new Vector();
			engine = new SwingEngine(this);
			container = (XDialog)engine.render(new File(Path.SYS_XML + "gui-login.xml"));
			fillComboBox();
			container.setVisible(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}